package com.company.service.impl;

import com.company.entity.User;
import com.company.service.UserService;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class UserServiceImpl implements UserService {
    Map users=new HashMap();
    @Override
    public int regist(User user) {
        if (exists(user.getUsername())){
            System.out.println("已存在该用户名");
            return 0;
        }else{
            add(user);
        }
        return 1;
    }

    @Override
    public int add(User user) {
        users.put(user.getUsername(),user.getPassword());
        return 0;
    }

    @Override
    public int save(User user) {
        return 0;
    }

    @Override
    public User login(String name, String pswd) {
        Set keys=users.keySet();
        Iterator it=keys.iterator();
        while (it.hasNext()){
            String key=(String) it.next();
           if (name.equals(key)){
               if (users.get(key).toString().equals(pswd)){
                   System.out.println("登录成功");
                   return new User(name,pswd);
               }
           }
        }
        return null;
    }



    public boolean exists(String username){
        Set keys=users.keySet();
        Iterator it=keys.iterator();
        while (it.hasNext()){
            String key=(String) it.next();
            if (username.equals(key)){
                return true;
            }
        }
        return false;

    }
}
